package com.ycxh.controller;

import com.ycxh.controller.dto.DirConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class PageController {

    @Autowired
    private FileController fileController;

    @Autowired
    private OAuthController oAuthController;

    /**
     * 跳转登录页
     * https://blog.csdn.net/Lordnow/article/details/124284479
     */
    @RequestMapping("/")
    public String hello() {
        return "redirect:/loginJQ";
    }

    /**
     * 首页
     */
    @RequestMapping("/index")
    public String index(Model model) throws Exception {
        List<DirConfig> list = fileController.getDirConfig();
        model.addAttribute("pathList", list);

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        model.addAttribute("nowDate", fmt.format(new Date()));
        return "deploy/index";
    }

    @RequestMapping("/auth")
    public String githubIndex(Model model) throws Exception {

        model.addAttribute("clientIdGithub", oAuthController.clientId);
        model.addAttribute("redirectUri", oAuthController.redirectUri);

        model.addAttribute("clientIdGitee", oAuthController.clientIdGitee);
        model.addAttribute("redirectUriGitee", oAuthController.redirectUriGitee);

        return "auth";
    }

    /**
     * 首页--使用jquery提交表单
     */
    @RequestMapping("/indexJQ")
    public String indexJQ(Model model) throws Exception {
        List<DirConfig> list = fileController.getDirConfig();
        model.addAttribute("pathList", list);

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        model.addAttribute("nowDate", fmt.format(new Date()));
        return "deploy/indexJQ";
    }

    /**
     * 登录页
     */
    @RequestMapping("/login")
    public String login(Model model) throws Exception {
        return "login";
    }

    /**
     * 登录页--使用jquery提交表单
     */
    @RequestMapping("/loginJQ")
    public String loginJQ(Model model, HttpSession session) throws Exception {
        Object object = session.getAttribute("user");
        if (object != null && object.toString().equals("admin")) {
            // 已登录，跳转首页
            // 重定向，改变浏览器url
            // return "redirect:indexJQ";
            // 工作台
            return "redirect:nav";
            // return "redirect:/deploy/indexJQ";
        }
        return "loginJQ";
    }

    /**
     * 文件上传
     */
    @RequestMapping("/uploadFile")
    public String uploadFile(Model model) throws Exception {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        model.addAttribute("nowDate", fmt.format(new Date()));
        return "uploadFile";
    }

    /**
     * 导航页
     */
    @GetMapping("/nav")
    public String nav(Model model) throws Exception {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        model.addAttribute("nowDate", fmt.format(new Date()));
        return "home-nav/index";
    }

}
